class DefaultController < ApplicationController
  layout 'scaffold'

  ## default permissions
  require_role :delete, :for => :destroy
  require_role :edit, :for => [:edit, :update_column]
  require_role :create, :for => [:new, :create]

  ActiveScaffold.set_defaults do |config|
    config.security.current_user_method = :current_login
    config.security.default_permission = true
    config.theme = :blue
    config.list.per_page = 20
    config.nested.shallow_delete = true
    config.ignore_columns.add [:created_at,:updated_at,:deleted_at,:created_by,:updated_by]
    config.list.mark_records = true
    config.list.page_links_window = 5
    ## actions
    config.actions.exclude :export, :config_list
    config.actions.exclude :delete, :show, :update, :create, :search
    config.actions.add :show, :delete, :update, :create, :search
  end

  def controller_before_create_update_save(record)
  end
  
  # active_scaffold/lib/active_scaffold/actions/create.rb
  def do_before_create_save(record)
    record.created_by = current_user.id
    controller_before_create_update_save(record)
  end
  # active_scaffold/lib/active_scaffold/actions/update.rb
  def do_before_update_save(record)
    record.updated_by = current_user.id
    controller_before_create_update_save(record)
  end

  ## variables
  SMALL_TEXTAREA = {:rows => '3', :cols => '50'}
  NOTE_TEXTAREA = {:rows => '6', :cols => '70'}
  TEXT_EDITOR = {:rows => '20', :cols => '90'}

  def self.active_scaffold_controller_for(klass)
    return Documenti::NoteController if klass == Nota
    return Documenti::AllegatiController if klass == Allegato
    return Anagrafiche::PartnersController if klass == Partner
    return Anagrafiche::PersonaleController if klass == Personale
    return Anagrafiche::VeicoliController if klass == Veicolo
    return Anagrafiche::AttrezziController if klass == Attrezzo
    return Tabelle::TipiAttrezzoController if klass == TipoAttrezzo
    return Personale::RapportiDiLavoroController if klass == RapportoDiLavoro
    return Personale::TagliePersonaleController if klass == TagliePersonale
    return Contabilita::CoordinateBancarieController if klass == CoordinateBancarie
    return Contabilita::PolizzeController if klass == Polizza
    return Personale::MansioniController if klass == Mansione
    return Tabelle::TipiVisitaMedicaController if klass == TipoVisitaMedica
    return Personale::RequisitiFormativiController if klass == RequisitoFormativo
    return Personale::RequisitiFormativiPersonaleController if klass == RequisitoFormativoPersonale
    return Resoconti::DettagliMovimentoMagazzinoController if klass == DettaglioMovimentoMagazzino
    return Resoconti::MovimentiMagazzinoController if klass == MovimentoMagazzino
    return Resoconti::ProdottiMagazzinoController if klass == ProdottoMagazzino
    return Resoconti::ManutenzioniAttrezzo if klass == ManutenzioneAttrezzo
    #return Tabelle::CentriCostoController if klass == CentroCosto
    return "#{klass}ScaffoldController".constantize rescue super
  end

  protected

  def do_create_authorized?
    current_user.has_role?('create')
  end
  def do_update_authorized?
    current_user.has_role?('edit')
  end
  def do_delete_authorized?
    current_user.has_role?('delete')
  end
  def do_export_authorized?
    current_user.has_role?('export')
  end
end
